package com.zb.store.controller;


import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.code.service.ConCodeService;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.SelectVo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjStoreOutboundService;
import com.zb.service.ZjUserService;
import com.zb.store.service.ConStoreOutboundService;
import com.zb.utils.TokenUtil;
import com.zb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * <p>
 * 出库表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2024-01-08
 */
@Api(tags = "仓库出库类接口")
@RestController
@RequestMapping("/store/outbound")
public class ZjStoreOutboundController {
    @Resource
    private ConStoreOutboundService conStoreOutboundService;
    @Resource
    private ConCodeService conCodeService;
    @Resource
    private ZjUserService userService;

    @ApiOperation("获取在库的一物一码")
    @PostMapping("/getInStoreCodeList")
    public Result getInStoreCodeList(HttpServletRequest request, @RequestBody @Valid CodeInStoreListSearchVo searchVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取在库的一物一码成功").data(conCodeService.getInStoreCodeList(custAccId, searchVo));
    }

    @ApiOperation("获取出库收货客户信息")
    @PostMapping("/getOutStoreReceiptsUser")
    public Result getOutStoreUser(HttpServletRequest request, @RequestBody SelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取出库收货客户信息成功").data(userService.getOutStoreUser(custAccId, selectVo));
    }

    @ApiOperation("保存或审核出库单")
    @PostMapping("/saveOrCheck/{submitType}")
    public Result saveOrCheck(HttpServletRequest request, @PathVariable @ApiParam(value = "提交类型：1-保存，2-审核", required = true) String submitType,
                              @RequestBody @Valid StoreOutboundVo outboundVo) {
        Assert.isTrue(Pattern.matches("[1|2]", submitType), "提交类型参数不合法");
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreOutboundService.saveOrCheck(loginInfo, submitType, null, outboundVo);
        return Result.success(Objects.equals("1", submitType)? "保存成功" : "审核出库成功");
    }

    @ApiOperation("出库列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody StoreOutboundListSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取出库列表成功").data(conStoreOutboundService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据出库单ID获取出库详情")
    @GetMapping("/getOutboundDetail/{id}")
    public Result getOutboundDetail(@PathVariable @ApiParam(value = "出库单ID", required = true) Long id) {
        return Result.success("获取出库详情成功").data(PageUtil.row(conStoreOutboundService.getOutboundDetail(id)));
    }

    @ApiOperation("根据出库单ID获取出库物料")
    @GetMapping("/getOutboundMaterial/{id}")
    public Result getOutboundMaterial(@PathVariable @ApiParam(value = "出库单ID", required = true) Long id) {
        return Result.success("获取出库物料成功").data(PageUtil.row(conStoreOutboundService.getOutboundMaterial(id)));
    }

    @ApiOperation("确认出库")
    @PostMapping("/saveSureStoreOutbound/{id}")
    public Result saveSureOutbound(HttpServletRequest request, @PathVariable @ApiParam(value = "出库单ID", required = true) Long id, @RequestBody @Valid StoreOutboundSendVo sendVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreOutboundService.saveSureOutbound(loginInfo, id, sendVo);
        return Result.success("出库成功");
    }

    @ApiOperation("作废出库单")
    @PostMapping("/cancelStoreOutbound/{id}")
    public Result cancelOutbound(HttpServletRequest request, @PathVariable @ApiParam(value = "出库单ID", required = true) Long id,
                                 @RequestBody @Valid StoreRemarkVo storeVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreOutboundService.cancelOutbound(loginInfo, id, storeVo.getRemark());
        return Result.success("作废出库单成功");
    }

    @ApiOperation("驳回出库单")
    @PostMapping("/rejectOutbound/{id}")
    public Result rejectOutbound(HttpServletRequest request, @PathVariable @ApiParam(value = "出库单ID", required = true) Long id,
                                 @RequestBody @Valid StoreRemarkVo storeVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conStoreOutboundService.rejectOutbound(loginInfo, id, storeVo.getRemark());
        return Result.success("驳回出库单成功");
    }





}
